package joc.internal.util;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;
import java.util.SortedSet;
import java.util.TreeSet;

public class SortedProperties extends Properties {
	private static final long serialVersionUID = 1L;
	private final SortedSet<Object> keys = new TreeSet<Object>();
	private final SortedSet<String> stringKeys = new TreeSet<String>();

	@Override
	public Enumeration<Object> keys() {
		return Collections.<Object> enumeration(keys);
	}

	@Override
	public Object put(Object key, Object value) {
		keys.add(key);
		stringKeys.add(key.toString());
		return super.put(key, value);
	}

	@Override
	public SortedSet<String> stringPropertyNames() {
		return stringKeys;
	}

	public SortedSet<String> stringPropertyNames(String prefix) {
		SortedSet<String> prefixKeys = new TreeSet<String>();
		for (String key : stringPropertyNames()) {
			if (key.startsWith(prefix)) {
				prefixKeys.add(key);
			}
		}
		return prefixKeys;
	}
}